<template>
	<view class=" ">
		<view class="page bg-white b_y">
			<view>
				<view class="f-d  bg-white p20 a-c  " style="padding-top: 40rpx;padding-bottom: 20rpx; ">
					<text class="f32 c_z">上传壁纸</text>
				</view>
				<view class="flex">
					<view v-if="shop_img" class="uni-uploader__input-box a-c j-c " style="margin-left: 20rpx;">
						<image class="b_y" style="height: 408rpx;width:280rpx;border: solid 2rpx #bfbfbf;"
							:src="shop_img|getImg"></image>
					</view>
					<view v-if="!shop_img" style="margin-left: 20rpx;" @tap="on_img_add(0)">
						<view class="flex a-c j-c" style="width: 280rpx;height: 408rpx;border: solid 2rpx #bfbfbf; ">
							<text class="bicon" style="font-size: 80rpx;color: #bfbfbf;">&#xe7dd;</text>
						</view>
					</view>
				</view>
				<view class="f-d  bg-white p20 a-c bb" style="padding-top: 40rpx;padding-bottom: 20rpx;">
					<view class="c_pl" style="width: 180rpx;">
						<text class="f28 c_z">图片名称</text>
					</view>
					<input class="f28 in" type="text" value="" placeholder="请输入图片名称" v-model="shop_name" />
				</view>
				<view class="f-d  bg-white p20 a-c bb " style="padding-top: 20rpx;">
					<view style="width: 180rpx;">
						<text class="f28 c_z">选择分类</text>
					</view>
					<view class="uni-list-cell-db">
						<lb-picker @confirm="on_navcate" :list="nav_list" :level="2" mode="multiSelector"
							:value="nav_list" :lei="2">
							<text class="f28 c_pl">{{cata_name}}</text>
						</lb-picker>
					</view>
				</view>
				<view class="f-d  bg-white p20  bb" style="width: 750rpx;">
					<view style="width: 150rpx;">
						<text class="f28 c_z">选择合集</text>
					</view>
					<view class="classify" style="width: 80%;">
						<view class="flex a-c j-c b_y  " @click="on_tags(index)" v-for="(item,index) in tag_list"
							:key="index" :class="item.is_selected?'tag_selected_is':'tag_selected'">
							<text class="f28">{{item.name}}</text>
						</view>
					</view>

				</view>
				<view class="p2">
					<text class="f28 cred">投稿说明</text>
				</view>
				<view class="" style="height: 150rpx;">

				</view>
			</view>
			<view class="flex j-c a-c">
				<button class="js-image-a" type="default "
					style="margin: 20upx;background-color: #0071FE;width: 600rpx;color: #fff;border-radius: 45rpx;"
					@tap="on_post">
					<text class=" p20">提交</text>
				</button>
			</view>

		</view>

	</view>
</template>

<script>
	import choose from "@/common/image/choose.vue"
	import compress from "@/common/image/compress.vue"




	export default {
		name: 'newsPublish',
		components: {
			choose,
			compress,


		},
		data() {
			return {
				isYasuo: true,
				imageUrl: '',
				show: false,
				url: '',
				isdingwei: true,
				img_list: [],
				nav_list: ['二手信息', '车辆信息', '房产信息', '招聘信息', '广告其他'],
				tag_list: [],

				list: [],
				cata_name: '请选择',
				shop_img: '',
				shop_tel: '',
				shop_tag: '',
				shop_addr: '',
				business_time: '请选择',
				shop_name: '',
				is_tupian: 0,
				selectLocatin: {
					location: {}
				},

				rendering: false,
				shop_tag1:[],
				id:''

			}
		},


		onLoad(on) {
			this.token = uni.getStorageSync('token');
			var pages = getCurrentPages()
			let currentRoute = pages[pages.length - 1].route;
			this.api_path = currentRoute.match(/pages\/(\S*)\//)[1];

        if(on.id){
			this.id = on.id;
		}
			 

			this.init();



		},
		methods: {
			on_tags(index) {
				var id = this.tag_list[index]['id'];
				var name = this.tag_list[index]['name'];
				if (this.shop_tag) {
					this.shop_tag = this.shop_tag + ',' + name
				} else {
					this.shop_tag = name
				}


				this.tag_list[index]['is_selected'] = !this.tag_list[index]['is_selected']

				this.$forceUpdate()
			},
			is_log() {
				uni.showModal({
					title: '未登陆',
					content: '登陆后可入住',
					confirmText: '去登陆',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/banmi/user_login'
							})
						}
					}
				});
			},
			async on_img_add(type) {
				let that = this;
				//选择图片
				const ret = await this.Bmupload.chooseImage();
				if (ret != 0) {
					this.Bmupload.banmi_upload({
						files: ret,
						onProgressUpdate: res => {
							console.log("上传进度: " + JSON.stringify(res));
						}
					}).then(res => {
						this.shop_img = res[0]

					});
				}
			},




			on_post() {
				this.token = uni.getStorageSync('token');
				if (!this.token) {
					this.is_log();
					return
				}


				if (!this.nav_id) {
					uni.showToast({
						title: "请选择分类",
						icon: 'none'
					})
					return
				}
				if (!this.shop_img) {
					uni.showToast({
						title: "请上传壁纸图片",
						icon: 'none'
					})
					return
				}
				let that = this
				var img_list = new Array();
				that.add_shop();




			},
			time(picker) {
				this.business_time = picker.value.join("-");
			},
			on_navcate(picker) {

				this.cata_name = picker.item[0];
				var index = picker.index[0];
				var data = this.NavList[index];
				this.nav_id = data['id'];

			},
			init() {
				var that = this;
				let requestParams = {
					id: this.id
				};
				this.Http.post('/plugin.php/banmichaoqiangbizhi/api/index/shop_edit', requestParams).then(ret => {
					if (ret.status == 200) {
						this.rendering = true;
						var NavList = ret.data.NavList;
						var shop_find = ret.data.find;


						if (shop_find) {
							this.id = shop_find.id
							this.shop_img = shop_find.shop_img
							this.shop_name = shop_find.shop_name
							this.shop_tag = shop_find.shop_tag;
							this.shop_tag1 =  shop_find.shop_tag.split(',');
						}




						this.NavList = NavList;
						var name = '';
						for (var i = 0; i < NavList.length; i++) {
							if (i == 0) {
								name += NavList[i]['name']
							} else {
								name += ',' + NavList[i]['name'];
							}
							if (shop_find && shop_find.nav_id == NavList[i]['id']) {
								this.cata_name = NavList[i]['name'];
								this.nav_id = NavList[i]['id'];
							}

						}
						this.nav_list = name.split(",");
						this.init_shop_tags();


					}
				})
			},
			init_shop_tags() {
				var that = this;
				let requestParams = {
					user_id: uni.getStorageSync('user_id'),
					token: uni.getStorageSync('token'),
				};
				this.Http.post('/plugin.php/banmichaoqiangbizhi/api/index/shop_tags', requestParams).then(ret => {
					if (ret.status == 200) {
						this.rendering = true;
						this.tag_list = ret.data.list.data;
						for (var i = 0; i < this.tag_list.length; i++) {
							this.tag_list[i]['is_selected'] = false
							for (var k = 0; k < this.shop_tag1.length; k++) {
								if (this.shop_tag1[k] == this.tag_list[i]['name']) {
									this.tag_list[i]['is_selected'] = true
								}
							}
						}
					}
				})
			},
			on_img() {
				this.token = uni.getStorageSync('token');

				if (!this.token) {
					this.is_log();
					return


				}
				var that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: res => {
						const tempFilePaths = res.tempFilePaths;
						that.shop_img = tempFilePaths[0];

					}
				});

			},
			add_shop() {
				var shop_img = this.shop_img.replace(this.img_url, "")
				let requestParams = {
					shop_name: this.shop_name,
					shop_img: shop_img,
					shop_tag: this.shop_tag,
					nav_id: this.nav_id,
					id: this.id
				};
				this.Http.post('/plugin.php/banmichaoqiangbizhi/api/index/shopAddajax', requestParams).then(ret => {
					uni.showToast({
						title: ret.msg,
						icon: 'none'
					})
					if (ret.status == 200) {
						setTimeout(function() {
							uni.redirectTo({
								url: '/pages/banmichaoqiangbizhi/my_shop'
							})
						}, 2000);

					}
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}

	.classify {
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
	}

	.tag_selected_is {
		background-color: #0071FE;
		flex: 0 0 21%;
		margin: 8rpx;
		color: #fff;
	}

	.tag_selected {
		background-color: #F5F5F5;
		flex: 0 0 21%;
		margin: 8rpx;
		color: #333;
	}

	.shuru {
		height: 250rpx;
		line-height: 50upx;
		width: 750rpx;
		border-width: 2rpx !important;
		border-right-style: solid !important;
		border-left-color: #FFFFFF !important;
		border-right-color: #FFFFFF !important;
		border-bottom-color: #D9D9D9 !important;
		border-top-color: #FFFFFF !important;

	}

	.input-view {
		font-size: 28upx;
	}

	.choose-location {
		padding: 30rpx;


	}


	.info-box {
		padding: 30rpx 0;

	}

	.info-box-item {
		padding-bottom: 20rpx;
	}

	.feedback-title {

		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding: 20upx;
		color: #8f8f94;
		font-size: 28upx;
	}



	.in {
		width: 500rpx;
		height: 50rpx;
	}

	.close-view {
		width: 35rpx;
		height: 35rpx;
		border-radius: 50%;
		background-color: #FF5053;
		color: #FFFFFF;
		position: relative;
		top: -110px;
		right: -85px;
		font-size: 20rpx;
	}
</style>